using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler;
using BlackStar;


namespace BlackStarPipes
{
    [ContentTypeWriter]
    public class FontListContentWriter : ContentTypeWriter<FontList>
    {
        protected override void Write(ContentWriter output, FontList value)
        {
            WriteFontList(output, value);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(FontListContentReader).AssemblyQualifiedName;
        }

        internal static void WriteFontList(ContentWriter output, FontList value)
        {
            output.Write(value.Count);
            foreach (KeyValuePair<string, Font> item in value)
            {
                output.Write(item.Key);
                output.WriteObject<Font>(item.Value);
            }
        }
    }

    [ContentTypeWriter]
    public class KeyItemContentWriter : ContentTypeWriter<KeyItem>
    {
        protected override void Write(ContentWriter output, KeyItem value)
        {
            output.Write(value.Plain);
            output.Write(value.Shift);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(KeyItemContentReader).AssemblyQualifiedName;
        }
    }

    [ContentTypeWriter]
    public class LabelContentWriter : ContentTypeWriter<Label>
    {
        protected override void Write(ContentWriter output, Label value)
        {
            WriteLabel(output, value);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(LabelContentReader).AssemblyQualifiedName;
        }

        internal static void WriteLabel(ContentWriter output, Label value)
        {
            ComponentContentWriter.WriteComponent(output, value);
        }
    }

    [ContentTypeWriter]
    public class MoveableComponentContentWriter : ContentTypeWriter<MoveableComponent>
    {
        protected override void Write(ContentWriter output, MoveableComponent value)
        {
            WriteMoveableComponent(output, value);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(MoveableComponentContentReader).AssemblyQualifiedName;
        }

        internal static void WriteMoveableComponent(ContentWriter output, MoveableComponent value)
        {
            ComponentContentWriter.WriteComponent(output, value);
            output.Write(value.Sizeable);
            output.Write(value.HandleSize);
            output.Write(value.Draggable);
            output.WriteObject<Rectangle>(value.DragHandle);
            output.Write((int)value.DragHandleDock);
            output.WriteObject<Point>(value.MinimumSize);
        }
    }

    [ContentTypeWriter]
    public class FormContentWriter : ContentTypeWriter<Form>
    {
        protected override void Write(ContentWriter output, Form value)
        {
            WriteForm(output, value);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(FormContentReader).AssemblyQualifiedName;
        }

        internal static void WriteForm(ContentWriter output, Form value)
        {
            MoveableComponentContentWriter.WriteMoveableComponent(output, value);
        }
    }

    [ContentTypeWriter]
    public class ComponentContentWriter : ContentTypeWriter<Component>
    {
        protected override void Write(ContentWriter output, Component value)
        {
            WriteComponent(output, value);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(ComponentContentReader).AssemblyQualifiedName;
        }

        internal static void WriteComponent(ContentWriter output, Component value)
        {
            output.Write(value.Name);
            output.WriteObject<Rectangle>(value.Rectangle);
            output.WriteObject<Rectangle>(value.Padding);
            output.Write(value.Color);
            output.Write(value.Visible);
            output.Write(value.Cursor);
            output.Write(value.Text);
            output.Write(value.Font);
            output.Write(value.FontColor);
            output.Write((int)value.Dock);
            output.Write(value.InheritColor);
            output.Write(value.InheritFont);
            output.Write(value.InheritFontColor);
            output.Write(value.TabNext);
            output.Write(value.TabPrev); 
            output.WriteObject<List<Component>>(value.Children);
        }
    }

    [ContentTypeWriter]
    public class FontContentWriter : ContentTypeWriter<Font>
    {
        protected override void Write(ContentWriter output, Font value)
        {
            output.Write(value.AssetName);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(FontContentReader).AssemblyQualifiedName;
        }
    }

    [ContentTypeWriter]
    public class ButtonContentWriter : ContentTypeWriter<Button>
    {
        protected override void Write(ContentWriter output, Button value)
        {
            WriteButton(output, value);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(ButtonContentReader).AssemblyQualifiedName;
        }

        internal static void WriteButton(ContentWriter output, Button value)
        {
            ComponentContentWriter.WriteComponent(output, value);
            // add button properties.
        }
    }

    [ContentTypeWriter]
    public class TextboxContentWriter : ContentTypeWriter<Textbox>
    {
        protected override void Write(ContentWriter output, Textbox value)
        {
            WriteTextbox(output, value);
            output.Write(value.HighlightBackColor);
            output.Write(value.HighlightForeColor);
            output.Write(value.PasswordChar);
        }

        public override string GetRuntimeReader(TargetPlatform targetPlatform)
        {
            return typeof(TextboxContentReader).AssemblyQualifiedName;
        }

        internal static void WriteTextbox(ContentWriter output, Textbox value)
        {
            LabelContentWriter.WriteLabel(output, value);
        }
    }
}
